package com.quanyue.music.system.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.music.domain.constant.FMChannel;
import com.quanyue.music.domain.constant.UserRole;
import com.quanyue.music.domain.entity.ExcelAutoError;
import com.quanyue.music.domain.entity.PlayList;
import com.quanyue.music.system.service.AuditionService;
import com.quanyue.music.system.service.PlayListService;
import com.quanyue.music.system.type.FMPlayDetail;
import com.quanyue.music.system.type.FMPlayList;
import com.quanyue.music.system.type.FMPlayRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tika.mime.MimeTypeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "播放列表")
@RestController
@RequestMapping("/api")
public class PlayListController {
    @Autowired
    private PlayListService playListService;
    @Autowired
    private AuditionService auditionService;

    @ApiOperation(value = "excel自动排期")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/fm/play/excel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <ExcelAutoError>> excelAdd(@RequestParam("file") MultipartFile file,
                                                          @RequestParam("fmChannel") FMChannel channel,
                                                          @RequestParam("data") String data) throws IOException, MimeTypeException {
        return ServiceResult.success(playListService.addExcel(file, channel, data));
    }

    @ApiOperation(value = "清空排期表")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/fm/play/clear", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> clearPlay(@RequestParam("date") String date,
                                             @RequestParam("fmChannel") FMChannel fmChannel) {
        playListService.clearPlay(date, fmChannel);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "设置FM播放列表")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/fm/play/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> add(@RequestBody PlayList playList) {
        playListService.add(playList);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改FM播放列表")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/fm/play/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> update(@RequestBody PlayList playList) {
        playListService.update(playList);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除FM播放列表")
    @Scope(role = UserRole.MANAGER)
    @DeleteMapping(value = "/fm/play/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> delete(@RequestParam("id") String id) {
        playListService.delete(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "校检文件")
    @Scope(role = UserRole.MANAGER)
    @GetMapping(value = "/fm/play/check", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> check(@RequestParam("date") String date,
                                         @RequestParam("fmChannel") FMChannel fmChannel) {
        return ServiceResult.success(playListService.check(date, fmChannel));
    }

    @ApiOperation(value = "播放列表展示")
    @Scope(role = UserRole.MANAGER)
    @GetMapping(value = "/fm/play/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <FMPlayList>> playList(@RequestParam("data") String date,
                                                      @RequestParam("fmChannel") FMChannel fmChannel) {
        return ServiceResult.success(playListService.findPlayList(date, fmChannel));
    }

    @ApiOperation(value = "15分钟免费试听记录")
    @Scope(role = UserRole.USER)
    @GetMapping(value = "/fm/audition", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> auditionRecord(@ApiIgnore Principal user) {
        auditionService.auditionRecord(user.getId());
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "当日试听记录查询")
    @Scope(role = UserRole.USER)
    @GetMapping(value = "/fm/audition/select", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> auditionRecordSelect(@ApiIgnore Principal user) {
       return ServiceResult.success(auditionService.auditionRecordSelect(user.getId()));
    }

    @ApiOperation(value = "查询要播出的歌曲、广告")
    @PostMapping(value = "/fm/play", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <FMPlayDetail>> findPlay(@RequestBody FMPlayRequest request, Principal user) {
        return ServiceResult.success(playListService.findPlay(request, user.getId()));
    }

    @ApiOperation(value = "查询会员到期时间")
    @GetMapping(value = "/fm/vip/end", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <LocalDateTime> findVipEndTime(@ApiIgnore Principal user) {
        return ServiceResult.success(playListService.findVipEndTime(user.getId()));
    }

}
